import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

/**
 * 
 * MysticGSpiderAFKer
 * 
 * @link http://www.rsbot.org/vb/showthread.php?t=57981
 * 
 * @Author Mouchicc < mourad94_4@hotmail.com >
 * 
 * @Date 2009
 * 
 * @param Updates
 *            : None
 * @version 1.0
 * 
 * 
 */
public class MysticMultiSkiller extends Script implements PaintListener {

	public int[] NORMALTREES = { 5004, 5005, 5045, 3879, 3881, 3882, 3883,
			3885, 3886, 3887, 3888, 3889, 3890, 3891, 3892, 3893, 3928, 3967,
			3968, 4048, 4049, 4050, 4051, 4052, 4053, 4054, 3033, 3034, 3035,
			3036, 2409, 2447, 2448, 1330, 1331, 1332, 1310, 1305, 1304, 1303,
			1301, 1276, 1277, 1278, 1279, 1280, 8742, 8743, 8973, 8974, 1315,
			1316 };
	public int[] OAKTREES = { 1281, 3037, 8462, 8463, 8464, 8465, 8466, 8467 };
	public int[] WILLOWTREES = { 1308, 5551, 5552, 5553, 8481, 8482, 8483,
			8484, 8485, 8486, 8487, 8488 };

	private final int[] NEST_IDS = { 5070, 5071, 5072, 5073, 5074, 5075, 5076,
			7413, 11966 };

	private final int[] LOG_IDS = { 1511, 1521, 1519, 1517, 1515, 1513, 12581,
			6332, 6333 };

	private final int[] CUT_ANIMS = { 867, 869, 871, 873, 875, 877, 879, 2846,
			1905, 1907, 10251, };

	// final points
	Point fletchPick;
	final Point NORMAL_SHORT_BOW = new Point(200, 415);
	final Point NORMAL_LONG_BOW = new Point(320, 410);
	final Point SHORT_BOW = new Point(100, 420);
	final Point LONG_BOW = new Point(255, 410);

	// Interfaces
	final RSInterface IFACE = RSInterface.getInterface(513);

	// final log ids
	final static int NORMAL = 1511;
	final static int OAK = 1521;
	final static int WILLOW = 1519;
	final static int MAPLE = 1517;
	final static int YEW = 1515;
	final static int MAGIC = 1513;

	// final knife id
	final static int KNIFE = 946;
	final static int CLAY_KNIFE = 14111;

	// final bow ids
	final static int NORMAL_SHORT = 50;
	final static int NORMAL_LONG = 48;
	final static int OAK_SHORT = 54;
	final static int OAK_LONG = 56;
	final static int WILLOW_SHORT = 60;
	final static int WILLOW_LONG = 58;

	int Axe;
	int Log;
	int ShortBow;
	int LongBow;
	public boolean shorty = false;
	public boolean longy = false;
	int[] BronzeAxe = { 1351, 1352 };
	int[] IronAxe = { 1349, 1350 };
	int[] SteelAxe = { 1353, 1354 };
	int[] BlackAxe = { 1361, 1362 };
	int[] MithrilAxe = { 1355, 1356 };
	int[] AddamantAxe = { 1357, 1358 };
	int[] RuneAxe = { 1359, 1360 };

	// ///////////////////////////////////////////////////////////////////////////

	public int YEWLOG = 1515;
	int[] YEW_LONGBOW_U = { 66, 67 };
	int[] YEW_LONGBOW = { 855, 856 };

	int[] MAGICTREES;
	int[] MAGICLOG = { 1513, 1514 };
	int[] MAGIC_LONGBOW_U = { 70, 71 };
	int[] MAGIC_LONGBOW;

	int[] keepIt = { Axe, Log, KNIFE };

	public int startLevel;
	public int startEXP;
	public long EXPGained;
	public int levelsGained;
	public long EXPPerHour;
	public String status = "Starting up";
	public boolean cutYews = false;
	public int yewPrice = 0;
	String whatTree;
	String whatMake;
	int whatLog = 0;

	// ***Ints and longs***\\
	int curStatExp;
	int startLvl;
	int startExp;
	int stat;
	int startxp;
	long lvls = 0;
	int startLVL;
	int CurSTAT;
	int RealSTAT;
	long startTime = System.currentTimeMillis();
	public int ITWID1 = 0000;
	public int ITWID2 = 0000;
	public int[] ITWIDs;
	public static int[] potID;
	int eatAT;
	public int fightMode;
	int runEnergy = random(25, 60);
	public static int[] FoodID = { 1895, 1893, 1891, 4293, 2142, 291, 2140,
			3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878,
			7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339,
			351, 329, 3381, 361, 379, 380, 10136, 5003, 379, 365, 373, 374,
			7946, 385, 397, 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327,
			2331, 2323, 2335, 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210,
			7218, 7220, 2003, 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301,
			2303, 1891, 1893, 1895, 1897, 1898, 1899, 1900, 1901, 1902, 7072,
			7062, 7078, 7064, 7084, 7082, 7066, 7068, 1942, 6701, 6703, 7054,
			6705, 7056, 7060, 2130, 1985, 1993, 1989, 1978, 5763, 5765, 1913,
			5747, 1905, 5739, 1909, 5743, 1907, 1911, 5745, 2955, 5749, 5751,
			5753, 5755, 5757, 5759, 5761, 2084, 2034, 2048, 2036, 2217, 2213,
			2205, 2209, 2054, 2040, 2080, 2277, 2225, 2255, 2221, 2253, 2219,
			2281, 2227, 2223, 2191, 2233, 2092, 2032, 2074, 2030, 2281, 2235,
			2064, 2028, 2187, 2185, 2229, 6883, 1971, 4608, 1883, 1885 };

	// ***Other***\\
	String logType;
	String skill;
	RSNPC GSpider = getNearestNPCByID(4400);
	public boolean doingspec;
	public boolean potion;
	public boolean attackSpiders;
	public boolean AFKSpiders;

	@Override
	public String getName() {
		return "MysticGSpiderAFKer";
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public String getAuthor() {
		return "Mouchicc - Mystic Team";
	}

	@Override
	public String getScriptCategory() {
		return "Combat";
	}

	public String getScriptDescription() {
		String html = "<html><head>";
		html += "</head><body>";
		html += "<center><strong><h2>" + getName() + " v" + getVersion()
				+ "</h2></strong></center>";
		html += "<strong>What tree you want to cut?</strong><br />";
		html += "<select name='whatTree'>";
		html += "<option>Normal";
		html += "<option>Oak";
		html += "<option>Willow";
		html += "<option>Maple</select><br />";
		html += "<strong>What you want to make?</strong><br />";
		html += "<select name='whatMake'>";
		html += "<option>Shafts";
		html += "<option>Shortbow";
		html += "<option>Longbow</select><br />";
		html += "<input name='sacredClay' type='checkbox' value='1'>Sacred Knife?<br />";
		html += "</body></html></center>";
		html += "";
		return html;
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onStart(Map<String, String> args) {
		Bot.getEventManager().addListener(PaintListener.class, this);
		boolean AFK = args.get("sacredClay") != null ? true : false;

		whatTree = args.get("whatTree");
		whatMake = args.get("whatMake");

		if (whatTree.equalsIgnoreCase("Normal")) {
			whatLog = NORMAL;
			if (whatMake.equalsIgnoreCase("Shafts")) {
				whatToMake = SHAFTS;
			} else if (whatMake.equalsIgnoreCase("Shortbow")) {
				whatToMake = SHORTS;
			} else if (whatMake.equalsIgnoreCase("Longbow")) {
				whatToMake = LONGS;
			}
		} else if (whatTree.equalsIgnoreCase("Oak")) {
			whatLog = OAK;
			if (whatMake.equalsIgnoreCase("Shortbow")) {
				whatToMake = SHORTS;
			} else if (whatMake.equalsIgnoreCase("Longbow")) {
				whatToMake = LONGS;
			}
		} else if (whatTree.equalsIgnoreCase("Willow")) {
			whatLog = WILLOW;
			if (whatMake.equalsIgnoreCase("Shortbow")) {
				whatToMake = SHORTS;
			} else if (whatMake.equalsIgnoreCase("Longbow")) {
				whatToMake = LONGS;
			}
		} else if (whatTree.equalsIgnoreCase("Maple")) {
			whatLog = MAPLE;
			if (whatMake.equalsIgnoreCase("Shortbow")) {
				whatToMake = SHORTS;
			} else if (whatMake.equalsIgnoreCase("Longbow")) {
				whatToMake = LONGS;
			}
		}

		log(getName() + " was successfully started!");
		return true;
	}

	// What to do when finished
	public void onFinish() {
		log("Thanks for using the script!");
		log("XP per hour: "
				+ ((skills.getCurrentSkillExp(stat) - startExp) * (3600000D / (((double) System
						.currentTimeMillis() - (double) startTime)))));
		log("Levels Gained: " + lvls);
		log("Experience Gained: "
				+ (skills.getCurrentSkillExp(stat) - startExp));
	}

	// Drinks potion
	public void drinkIt() {
		CurSTAT = skills.getCurrentSkillLevel(stat);
		RealSTAT = skills.getRealSkillLevel(stat);
		if (CurSTAT - RealSTAT <= random(1, 2) && potion == true) {
			for (final int potion : potID) {
				atInventoryItem(potion, "Drink");
				return;
			}
		} else {
			wait(random(20, 30));
			return;
		}
	}

	// Gets Special
	public int getSpec() {
		return getSetting(300);
	}

	// Gets if Special is Enabled
	public boolean specEnabled() {
		return (getSetting(301)) == 1;
	}

	// Does Special!
	public void doSpec() {
		if (getSpec() >= 1000 && !specEnabled() && doingspec == true) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
			wait(350);
		}
		return;
	}

	// Easy method for eating
	private boolean check_and_eat() {
		final int MyHP = skills.getCurrentSkillLevel(STAT_HITPOINTS);
		for (final int food : FoodID) {
			if (MyHP <= eatAT && getInventoryCount(food) >= 1) {
				atInventoryItem(food, "Eat");
			} else {
				wait(random(20, 30));
			}
		}
		return true;
	}

	// Sets Running and does Special and/or Eating if is in Combat
	public void thisFirst() {
		drinkIt();
		doSpec();
		check_and_eat();
		setFightMode(fightMode);

		if (getEnergy() >= runEnergy) {
			if (!isRunning()) {
				setRun(true);
			}
		}
	}

	private boolean inventoryContainsAny(int... items) {
		for (int item : getInventoryArray())
			for (int id : items)
				if (item == id)
					return true;
		return false;
	}

	private boolean equipmentContainsAny(int... items) {
		for (int item : getEquipmentArray())
			for (int id : items)
				if (item == id)
					return true;
		return false;
	}

	private boolean playerHasAny(int... items) {
		return inventoryContainsAny(items) || equipmentContainsAny(items);
	}

	public boolean animIs(int... anims) {
		int curAnim = getMyPlayer().getAnimation();
		for (int animation : anims) {
			if (curAnim == animation) {
				return true;
			}
		}
		return false;
	}

	public int fletchThatShit() {
		int wait1 = 250;
		int wait2 = 500;
		if (IFACE.isValid()) {
			clickMouse(fletchPick, false);
			wait(random(wait1, wait2));
			int count = random(30, 100);
			if (count > 10) {
				atMenu("Make X");
				wait(random(wait1 * 3, wait2 * 3));
				sendText("" + count, true);
			} else {
				atMenu("10");
			}
		} else {
			clickMouse(579, 228, true);
			wait(random(wait1, wait2));
			clickMouse(700, 445, true);
			wait(random(wait1 * 2, wait2 * 2));
		}

		return random(50, 150);
	}

	public int chopThatShit() {
		if (whatTree.equals("Normal")) {
			if (!animIs(CUT_ANIMS)) {
				chopDown(NORMALTREES);
			}
		} else if (whatTree.equals("Oak")) {
			if (!animIs(CUT_ANIMS)) {
				chopDown(OAKTREES);
			}
		} else if (whatTree.equals("Willow")) {
			if (!animIs(CUT_ANIMS)) {
				chopDown(WILLOWTREES);
			}
		}
		return random(50, 150);
	}

	@Override
	public int loop() {

		if (getInventoryCount(Log) >= 28) {
			fletchThatShit();
		} else if (longy && getInventoryCount(LongBow) >= 28) {
			dropAllExcept(keepIt);
		} else if (!isInventoryFull()) {
			chopThatShit();
		}

		return (random(500, 1000));
	}

	public boolean chopDown(int[] tree) {
		RSObject Tree = getNearestObjectByID(tree);
		final RSTile treeLoc = Tree.getLocation();

		if (Tree == null) {
			return false;
		} else if (distanceTo(Tree) > random(4, 5)) {
			walkTileMM(treeLoc);
			wait(random(200, 500));
		} else {
			atTree(Tree, "Chop down");
		}
		return false;
	}

	// Paint of Script, Credits to Noobielul
	public void onRepaint(Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			long hours = millis / (1000 * 60 * 60);
			millis -= hours * (1000 * 60 * 60);
			long minutes = millis / (1000 * 60);
			millis -= minutes * (1000 * 60);
			long seconds = millis / 1000;

			g.setColor(new Color(0, 0, 0, 100));
			g.fillRoundRect(10, 209, 110, 123, 10, 10);

			long totalSeconds = ((System.currentTimeMillis() - startTime) / 1000);

			int y = 211;
			EXPGained = skills.getCurrentSkillExp(8) - startEXP;
			levelsGained = skills.getCurrentSkillLevel(8) - startLevel;
			if (totalSeconds == 0) {
				EXPPerHour = 0;
			} else {
				EXPPerHour = (EXPGained * 3600) / totalSeconds;
			}

			long secondsToLvl;

			if (EXPPerHour == 0) {
				secondsToLvl = 0;
			} else {
				secondsToLvl = (skills.getXPToNextLevel(8) * 3600) / EXPPerHour;
			}
			;
			long hoursToLvl = secondsToLvl / (60 * 60);
			secondsToLvl -= hoursToLvl * (60 * 60);
			long minutesToLvl = secondsToLvl / (60);
			secondsToLvl -= minutesToLvl * (60);

			g.setFont(new Font("Arial", Font.BOLD, 12));
			g.setColor(Color.green);
			g.drawString(status, 15, y += 14);
			g.setColor(Color.white);
			g.drawString("Runtime: " + hours + ":" + minutes + ":" + seconds,
					15, y += 14);
			if (cutYews == false) {
				g.drawString("Gained: " + EXPGained, 15, y += 14);
			}
			g.drawString("Levels Gained: " + levelsGained, 15, y += 14);
			g.drawString("Current Level: " + skills.getCurrentSkillLevel(8),
					15, y += 14);
			if (cutYews == false) {
				g.drawString("EXP/Hour: " + EXPPerHour, 15, y += 14);
			}
			g.drawString("Level in: " + hoursToLvl + ":" + minutesToLvl + ":"
					+ secondsToLvl, 15, y += 14);
			g.setColor(new Color(255, 0, 0, 140));
			g.fillRoundRect(15, y += 5, 100, 12, 10, 12);
			g.setColor(new Color(0, 255, 0, 140));
			g.fillRoundRect(15, y, skills.getPercentToNextLevel(8), 12, 10, 12);
			g.setColor(Color.white);
			g.drawString("" + skills.getPercentToNextLevel(8) + "%", 55,
					y += 10);
		}
	}
}